package com.zprmb.udzt;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.adapter.Funds_adapter;
import com.zprmb.udzt.bean.FundsBean;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.ToJSON;
import com.zprmb.udzt.util.VolleryUtil;
/**
 * 资金流水
 * @author shenshao
 *
 */
public class Funds_flow extends BaseActivity implements
		OnRefreshListener2<ListView>, OnClickListener {
	private PullToRefreshListView lv;
	private List<FundsBean> list, list2;
	private String mytype = "ALL";
	private ListView listView;
	private TextView tv_filter;
	private PopupWindow pop;
	private Button filter_all, filter_recharge, filter_withdraw,
			filter_investment, filter_repayment, filter_hongbao,
			filter_commission, tv_example_gold,tv_example_coupon;
	private Funds_adapter adapter;
	private int page=1;
	private ImageView img_back,img_none;
	private RelativeLayout rl_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.funds_flow);
		 BbtApplication.getInstance().addActvity(this);
		initview();
		initData();

	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	private void initview() {
		// TODO Auto-generated method stub
		lv = (PullToRefreshListView) findViewById(R.id.lv_funds_flow);
		listView = lv.getRefreshableView();
		registerForContextMenu(listView);
		lv.setOnRefreshListener(this);
		listView.setSelection(0);
        img_back=(ImageView)findViewById(R.id.img_back);
        img_none=(ImageView)findViewById(R.id.iv_v4_nonetwork);
        img_back.setOnClickListener(this);
		tv_filter = (TextView) findViewById(R.id.tv_filter);
		tv_filter.setOnClickListener(this);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		rl_back.setOnClickListener(this);
		list = new ArrayList<FundsBean>();
		list2 = new ArrayList<FundsBean>();
		lv.setOnRefreshListener(this);
		lv.setMode(Mode.BOTH);
		View view = LayoutInflater.from(this)
				.inflate(R.layout.pop_filter, null);
		pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		pop.setOutsideTouchable(true);
		pop.setBackgroundDrawable(new BitmapDrawable());
		filter_all = (Button) view.findViewById(R.id.all);
		filter_commission = (Button) view.findViewById(R.id.commission);
		filter_investment = (Button) view.findViewById(R.id.invest);
		filter_hongbao = (Button) view.findViewById(R.id.red);
		filter_repayment = (Button) view.findViewById(R.id.invest_repay);
		filter_recharge = (Button) view.findViewById(R.id.deposit);
		filter_withdraw = (Button) view.findViewById(R.id.withdraw);
		tv_example_gold = (Button) view.findViewById(R.id.example_gold);
		tv_example_coupon=(Button)view.findViewById(R.id.example_coupon);
		tv_example_coupon.setOnClickListener(this);
		filter_all.setOnClickListener(this);
		filter_commission.setOnClickListener(this);
		filter_hongbao.setOnClickListener(this);
		filter_investment.setOnClickListener(this);
		filter_recharge.setOnClickListener(this);
		filter_repayment.setOnClickListener(this);
		filter_withdraw.setOnClickListener(this);
		tv_example_gold.setOnClickListener(this);

	}

	private void initData() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Gson gson=new Gson();
				Log.e("资金流水", jsonObject.toString());
				ToJSON.initData2(jsonObject.toString(), "zijinliushui");
				if(jsonObject.optInt("success")==1){
					if (!jsonObject.optString("data").equals("null")) {
						list=gson.fromJson(jsonObject.optString("data"), new TypeToken<ArrayList<FundsBean>>() {
							}.getType());
						
					}
					list2.addAll(list);
					adapter=new Funds_adapter(list2,Funds_flow.this,mytype);
					lv.setAdapter(adapter);
					
					adapter.notifyDataSetChanged();
				listView.setSelection(list2.size()-10);
				if(list2.size()<1){
					lv.setVisibility(View.GONE);
					img_none.setVisibility(View.VISIBLE);
				}else{
					lv.setVisibility(View.VISIBLE);
					img_none.setVisibility(View.GONE);
				}
				
				Log.e("当前type", mytype);
				}else {
					if (jsonObject.optString("comment").equals("please login")) {
						Intent intent = new Intent(Funds_flow.this,LoginActivity.class);
						startActivity(intent);
					}else{
						Toast.makeText(Funds_flow.this, jsonObject.optString("comment"), 2000).show();
					}
				}
				
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setShow(true);
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setHeaders("client", "android");
		volleryUtil.execute(Funds_flow.this, Constant.URL
				+ "/api/v2/queryFunds?pageNo="+page+"&pageSize=10&type=" + mytype, "");
		Log.e("流水", mytype);

	} 

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(1);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(2);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_filter:// 弹出
			pop.showAsDropDown(v);
			break;
		case R.id.all:// 全部
			page=1;
			list.clear();
			list2.clear();
			mytype="ALL";
			initData();
			pop.dismiss();
			break;
		case R.id.deposit:// 充值
			page=1;
			list.clear();
			list2.clear();
			mytype="DEPOSIT";
			initData();
			pop.dismiss();
			break;
		case R.id.withdraw:// 提现
			page=1;
			list.clear();
			list2.clear();
			mytype="WITHDRAW";
			initData(); 
			pop.dismiss();
			break;
		case R.id.invest:// 投资
			page=1;
			list.clear();
			list2.clear();
			mytype="INVEST";
			page=1;
			initData();
			pop.dismiss();
			break;
		case R.id.red:// 红包
			page=1;
			list.clear();
			list2.clear();
			mytype="RED";
		
			initData();
			pop.dismiss();
			break;
		case R.id.commission:// 佣金
			page=1;
			list.clear();
			list2.clear();
			mytype="OTHER";
		
			initData();
			pop.dismiss();
			break;
		case R.id.invest_repay:// 还款
			page=1;
			list.clear();
			list2.clear();
			mytype="INVEST_REPAY";
		
			initData();
			pop.dismiss();
			break;
		case R.id.example_gold://体验金
			page=1;
			list.clear();
			list2.clear();
			mytype="USER_EXPERIENCE_INTEREST";
		
			initData();
			pop.dismiss();


			break;
		case R.id.example_coupon:
			page=1;
			list.clear();
			list2.clear();
			mytype="RAISE";
		
			initData();
			pop.dismiss();
			break;
		case R.id.img_back:
			finish();
			break;
		case R.id.rl_back:
			finish();
			break;

		}
	}
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			list.clear();
			if(msg.what==1){
				page=1;
				list2.clear();
				Log.e("刷新type", mytype);
				initData();
			}else{
				page+=1;
				initData();
				
				
			}
			Toast.makeText(Funds_flow.this, "刷新成功", 2000).show();
			lv.onRefreshComplete();
		};
	};
}
